package com.codi.api.controller.organ;

import com.codi.base.exception.BaseAppException;
import com.codi.base.exception.ExceptionHandler;
import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.ListUtil;
import com.codi.bus.constant.FundConst;
import com.codi.bus.core.domain.FundInfo;
import com.codi.bus.core.resultModel.BaseListResult;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.resultModel.FundSortResult;
import com.codi.bus.core.service.FundDetailService;
import com.codi.bus.core.service.FundInfoService;
import com.codi.bus.core.service.model.OrganBabyModel;
import com.codi.bus.core.service.organ.OrganBabyService;
import com.codi.bus.core.service.util.FundUtil;
import com.codi.message.constant.ErrorConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lvgo
 * @version 1.0
 * @Description: 机构首页
 * @date 2018/6/6 16:00
 */
@RestController
@RequestMapping("/organ")
@Slf4j
public class OrganIndexController {
    @Resource(name = "fundDetailService")
    private FundDetailService fundDetailService;
    @Resource(name = "fundInfoService")
    private FundInfoService fundInfoService;
    @Resource
    private OrganBabyService organBabyService;

    /**
     * 企业宝列表
     *
     * @return
     */
    @Loggable
    @GetMapping("/qiyebaoList")
    public BaseResult getOrganBabies(Integer beginNum, Integer requestNum) {
        OrganBabyModel commentBaby = organBabyService.getCommentBaby();
        List<OrganBabyModel> organBabies = organBabyService.getOrganBabies();
        organBabies = organBabies.stream().filter(organBabyModel -> !commentBaby.getFundCode().equals(organBabyModel.getFundCode())).collect(Collectors.toList());
        ArrayList<OrganBabyModel> organBabyModels = new ArrayList<>();
        for (int i = 1; i <= organBabies.size(); i++) {
            if (i >= beginNum && i < requestNum + beginNum) {
                organBabyModels.add(organBabies.get(i - 1));
            }
        }
        BaseListResult result = new BaseListResult();
        Map<String, Object> resultMap = new HashMap<>();
        ArrayList<OrganBabyModel> commentBabys = new ArrayList<>();
        commentBabys.add(commentBaby);
        resultMap.put("recommend", commentBabys);
        resultMap.put("organBabies", organBabyModels);
        result.setResult(resultMap);
        result.setTotalCount((long) organBabies.size());
        result.setRowCount((long) organBabies.size());
        return result;
    }


    /**
     * 机构短期理财
     *
     * @throws BaseAppException
     */
    @Loggable
    @GetMapping("/short/financing")
    public BaseResult shortFinancing() throws BaseAppException {
        //基金类型编码
        Integer fundTypeCode = 1109;
        //统计类型 , 0-9: 日,周,月,季,半年,一年,两年,三年,今年以来,成立以来(货币类型和短期理财忽略此参数)
        Integer statisType = 0;
        Integer sort = 1;
        Integer pageIndex = 1;
        Integer pageSize = 3;
        switch (fundTypeCode) {
            case 0:
            case FundConst.FUND_TYPE_CODE_STOCK:
            case FundConst.FUND_TYPE_CODE_MIXIN:
            case FundConst.FUND_TYPE_CODE_BOND:
            case FundConst.FUND_TYPE_CODE_INDEX:
            case FundConst.FUND_TYPE_CODE_CURRENCY:
                // case FundConst.FUND_TYPE_CODE_GUARANTEE:保本型合并到混合类型中
            case FundConst.FUND_TYPE_CODE_QDII:
            case FundConst.FUND_TYPE_CODE_SHORT_TERM_FINANCING:
                break;
            default:
                ExceptionHandler.publish(ErrorConst.COMMON_INVALID_FUND_TYPE_CODE);
                break;
        }

        boolean isCurrency = FundUtil.isCurrency(fundTypeCode);
        FundSortResult result = new FundSortResult();
        result.setSuccess(true);
        // fundType=0,查询所有在售基金的排序（除了货基）
        // 查询出基金的InnerCodes
        List<String> fundCodes = null;
        List<FundInfo> fundInfos = fundInfoService.queryFundInfoByType(fundTypeCode);

        if (ListUtil.isEmpty(fundInfos)) {
            return result;
        } else {
            fundCodes = new ArrayList<>(fundInfos.size());
            for (FundInfo fund : fundInfos) {
                if (fund.getFundTypeCode() == null) {
                    log.warn("fundCode={},fundName={} has no fund type code", fund.getFundCode(), fund.getFundName());
                    continue;
                }
                fundCodes.add(fund.getFundCode());
            }
        }

        result.setSortList(fundDetailService.getSortByNetValuePerformance(fundTypeCode, fundCodes, statisType, sort,
            pageIndex, pageSize));

        return result;
    }

    /**
     * 获取企业宝推荐
     *
     * @return
     */
    @Loggable
    @GetMapping("/index/qiyebao")
    public BaseResult getOrganBaby() {
        BaseResult result = new BaseResult();
        OrganBabyModel commentBaby = organBabyService.getCommentBaby();
        result.setResult(commentBaby);
        return result;
    }


}
